package com.kingwang.training.designpattern.composite.bo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class Organization implements OrganizationNode {
    private List<OrganizationNode> children = new ArrayList<>();

    private String name;

    public Organization(String name) {
        this.name = name;
    }

    @Override
    public void add(OrganizationNode org) {
        this.children.add(org);
    }

    @Override
    public void remove(OrganizationNode org) {
        this.children.remove(org);
    }

    @Override
    public OrganizationNode getChild(int index) {
        if (this.children.size() > index) {
            return this.children.get(index);
        }

        return null;
    }

    @Override
    public void visit() {
        System.out.println(this.name + " 被访问");

        for (OrganizationNode node : this.children) {
            node.visit();
        }
    }
}
